﻿Imports OMANE.Common.UI
Imports OMANE.Common
Imports System.ComponentModel
Imports System.Xml.Serialization
Imports System.Windows.Input
Imports System.Collections.ObjectModel
Imports PTIC_Util

Imports OMANE.Common.Model.Interfaces

Namespace ViewModels
    <Serializable()> _
    Public Class ViewModelDonneesEconomiques
        Inherits ViewModelCommand
        Implements IViewModelTabSection

        Private _model As ModelTauxGarantis
        Public Property IsClosable As Boolean Implements IViewModelTabSection.IsClosable


        Public Sub New(ByVal Model As ModelTauxGarantis, ByVal DisplayName As String, ByVal Command As ICommand)
            MyBase.New(DisplayName, Command)
            _model = Model
            SegmentProvider.model = Model
            Title = "DONNEES ECONOMIQUES"

            m_GrilleTarifaires = New ReadOnlyCollection(Of IInfoTableReferentiel)(
                (From elt As IInfoTableReferentiel In _model.Referentiel.InfoTables
                 Select elt
                 Where elt.TypeTable = "TARIF").ToList)

            'Dim temp = New DataView()
            'temp.Table = _model.Univers.refListeTablesReferentiel
            'temp.RowFilter = "strTypeTable='TARIF'"
            '_GrilleTarifaires = temp

            m_GrilleTarifaireSelectionne = (From e As IInfoTableReferentiel In m_GrilleTarifaires
                                           Where e.NomFichier = _model.ListeParametres.GrilleTarifaire.ValeurUnique
                                           Select e).FirstOrDefault()
            AddHandler _model.ListeParametres.GrilleTarifaire.hasChanged, AddressOf GrilleTarifaire_HasChanged
            'Lorsque la grille tarifaire sélectionnée change, il faut mettre à jour le selectedItem de la comboBox
        End Sub
        Sub New()
        End Sub

#Region "IViewModelTabSection"
        Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone
        Public Property Title As String Implements Common.UI.IViewModelTabSection.Title
#End Region

#Region "Taxe d'assurance"
        Public Property TaxeDassurance As Double
            Get
                Return _model.ListeParametres.Tx_TaxeAssu.ValeurUnique
            End Get
            Set(ByVal value As Double)
                OnPropertyChanging("TaxeDassurance")
                _model.ListeParametres.Tx_TaxeAssu.ValeurUnique = Math.Max(value, 0)
                OnPropertyChanged("TaxeDassurance")
            End Set
        End Property
        Public ReadOnly Property TaxeDassurance_IsUsed As Boolean
            Get
                Return _model.ListeParametres.Tx_TaxeAssu.Methode_Cal(_model.numMethodeCalculEnCours - 1)
            End Get
        End Property
#End Region

#Region "Taux technique annuel"
        Public Property TauxTechniqueAnnuel As Double
            Get
                Return _model.ListeParametres.Tx_TechA.ValeurUnique
            End Get
            Set(ByVal value As Double)
                OnPropertyChanging("TauxTechniqueAnnuel")
                _model.ListeParametres.Tx_TechA.ValeurUnique = Math.Max(value, 0)
                OnPropertyChanged("TauxTechniqueAnnuel")
            End Set
        End Property
        Public ReadOnly Property TauxTechniqueAnnuel_IsUsed As Boolean
            Get
                Return _model.ListeParametres.Tx_TechA.Methode_Cal(_model.numMethodeCalculEnCours - 1)
            End Get
        End Property
#End Region

#Region "Taux de remboursements anticipés"
        Public Property TauxRemboursementAnticipe As Double
            Get
                Return _model.ListeParametres.Tx_RembAntiA.ValeurUnique
            End Get
            Set(ByVal value As Double)
                OnPropertyChanging("TauxRemboursementAnticipe")
                _model.ListeParametres.Tx_RembAntiA.ValeurUnique = Math.Max(value, 0)
                OnPropertyChanged("TauxRemboursementAnticipe")
            End Set
        End Property
        Public ReadOnly Property TauxRemboursementAnticipe_IsUsed As Boolean
            Get
                Return _model.ListeParametres.Tx_RembAntiA.Methode_Cal(_model.numMethodeCalculEnCours - 1)
            End Get
        End Property
#End Region

#Region "Taux de rendement annuel des actifs"
        Public Property TauxRendementActifs As Double
            Get
                Return _model.ListeParametres.Tx_RembAntiA.ValeurUnique
            End Get
            Set(ByVal value As Double)
                OnPropertyChanging("TauxRendementActifs")
                _model.ListeParametres.Tx_RendActifsA.ValeurUnique = Math.Max(value, 0)
                OnPropertyChanged("TauxRendementActifs")
            End Set
        End Property
        Public ReadOnly Property TauxRendementActifs_IsUsed As Boolean
            Get
                Return _model.ListeParametres.Tx_RendActifsA.Methode_Cal(_model.numMethodeCalculEnCours - 1)
            End Get
        End Property
#End Region

#Region "Taux d'imposition sur les sociétés"
        Public Property TauxImposition As Double
            Get
                Return _model.ListeParametres.Tx_TaxeImpoA.ValeurUnique
            End Get
            Set(ByVal value As Double)
                OnPropertyChanging("TauxImposition")
                _model.ListeParametres.Tx_TaxeImpoA.ValeurUnique = Math.Max(value, 0)
                OnPropertyChanged("TauxImposition")
            End Set
        End Property
        Public ReadOnly Property TauxImposition_IsUsed As Boolean
            Get
                Return _model.ListeParametres.Tx_TaxeImpoA.Methode_Cal(_model.numMethodeCalculEnCours - 1)
            End Get
        End Property
#End Region

#Region "Taux de rentabilité annuel exigé par les actionnaires"
        Public Property TauxExigeActionnaires As Double
            Get
                Return _model.ListeParametres.Tx_RendActionnairesA.ValeurUnique
            End Get
            Set(ByVal value As Double)
                OnPropertyChanging("TauxExigeActionnaires")
                _model.ListeParametres.Tx_RendActionnairesA.ValeurUnique = Math.Max(value, 0)
                OnPropertyChanged("TauxExigeActionnaires")
            End Set
        End Property
        Public ReadOnly Property TauxExigeActionnaires_IsUsed As Boolean
            Get
                Return _model.ListeParametres.Tx_RendActionnairesA.Methode_Cal(_model.numMethodeCalculEnCours - 1)
            End Get
        End Property
#End Region

#Region "Coût annuel des emprunts subordonnées"
        Public Property CoutAnnuelEmpruntsSubordonnes As Double
            Get
                Return _model.ListeParametres.Tx_CoutEmprSuboA.ValeurUnique
            End Get
            Set(ByVal value As Double)
                OnPropertyChanging("CoutAnnuelEmpruntsSubordonnes")
                _model.ListeParametres.Tx_CoutEmprSuboA.ValeurUnique = Math.Max(value, 0)
                OnPropertyChanged("CoutAnnuelEmpruntsSubordonnes")
            End Set
        End Property
        Public ReadOnly Property CoutAnnuelEmpruntsSubordonnes_IsUsed As Boolean
            Get
                Return _model.ListeParametres.Tx_CoutEmprSuboA.Methode_Cal(_model.numMethodeCalculEnCours - 1)
            End Get
        End Property
#End Region

#Region "Grille tarifaire"

        Private Sub GrilleTarifaire_HasChanged(ByVal sender As Object, ByVal e As EventArgs)
            m_GrilleTarifaireSelectionne = (From elt As IInfoTableReferentiel In m_GrilleTarifaires
                                            Where elt.NomFichier = _model.ListeParametres.GrilleTarifaire.ValeurUnique
                                            Select elt).FirstOrDefault()
            'Récupérer la valeur sélectionnée dans la liste de choix à partir du nom du fichier
            OnPropertyChanged("GrilleTarifaireSelectionne")
        End Sub

        Private m_GrilleTarifaireSelectionne As IInfoTableReferentiel
        Private m_GrilleTarifaires As ReadOnlyCollection(Of IInfoTableReferentiel)
        Public ReadOnly Property GrilleTarifaires As ReadOnlyCollection(Of IInfoTableReferentiel)
            Get
                Return m_GrilleTarifaires
            End Get
        End Property
        Public Property GrilleTarifaireSelectionne As IInfoTableReferentiel
            Get
                Return m_GrilleTarifaireSelectionne
            End Get
            Set(ByVal value As IInfoTableReferentiel)
                OnPropertyChanging("GrilleTarifaireSelectionne")
                m_GrilleTarifaireSelectionne = value
                _model.ListeParametres.GrilleTarifaire.ValeurUnique = m_GrilleTarifaireSelectionne.NomFichier
                OnPropertyChanged("GrilleTarifaireSelectionne")
            End Set
        End Property
        Public ReadOnly Property GrilleTarifaire_IsUsed As Boolean
            Get
                Return _model.ListeParametres.GrilleTarifaire.Methode_Cal(_model.numMethodeCalculEnCours - 1)
            End Get
        End Property
#End Region

#Region "Fraise de gestion"
        Public Property FraisGestion As Double
            Get
                Return _model.ListeParametres.Tx_fraiGestA.ValeurUnique
            End Get
            Set(ByVal value As Double)
                OnPropertyChanging("FraisGestion")
                _model.ListeParametres.Tx_fraiGestA.ValeurUnique = Math.Max(value, 0)
                OnPropertyChanged("FraisGestion")
            End Set
        End Property
        Public ReadOnly Property FraisGestion_IsUsed As Boolean
            Get
                Return _model.ListeParametres.Tx_fraiGestA.Methode_Cal(_model.numMethodeCalculEnCours - 1)
            End Get
        End Property
#End Region

#Region "Taux des emprunts subordonnés"
        Public Property TauxEmpruntsSubordonnes As Double
            Get
                Return _model.ListeParametres.Tx_CouvEmprSuboA.ValeurUnique
            End Get
            Set(ByVal value As Double)
                OnPropertyChanging("TauxEmpruntsSubordonnes")
                _model.ListeParametres.Tx_CouvEmprSuboA.ValeurUnique = Math.Max(value, 0)
                OnPropertyChanged("TauxEmpruntsSubordonnes")
            End Set
        End Property
        Public ReadOnly Property TauxEmpruntsSubordonnes_IsUsed As Boolean
            Get
                Return _model.ListeParametres.Tx_CouvEmprSuboA.Methode_Cal(_model.numMethodeCalculEnCours - 1)
            End Get
        End Property
#End Region

#Region "Ratio fonds propres sur MSR"
        Public Property RatioFondsSurMSR As Double
            Get
                Return _model.ListeParametres.Tx_FondPropSurMSR.ValeurUnique
            End Get
            Set(ByVal value As Double)
                OnPropertyChanging("RatioFondsSurMSR")
                _model.ListeParametres.Tx_FondPropSurMSR.ValeurUnique = Math.Max(value, 0)
                OnPropertyChanged("RatioFondsSurMSR")
            End Set
        End Property
        Public ReadOnly Property RatioFondsSurMSR_IsUsed As Boolean
            Get
                Return _model.ListeParametres.Tx_FondPropSurMSR.Methode_Cal(_model.numMethodeCalculEnCours - 1)
            End Get
        End Property
#End Region

        Protected Overrides Sub OnPropertyChanging(ByVal propertyName As String)
            If Not changingfromundoredo Then
                Dim urs = ModuleTauxGaranti._app.GetService(Of IUndoRedoService)()
                Dim prop = TypeDescriptor.GetProperties(Me)(propertyName)
                urs.Push( _
                    New UndoRedoToken( _
                        AddressOf UndoRedoMethod, _
                        New Object() {propertyName, prop.GetValue(Me)}, _
                        "Set Value To " & prop.ComponentType.Name & "." & prop.Name))
            End If
            MyBase.OnPropertyChanging(propertyName)
        End Sub

        Private changingfromundoredo As Boolean = False
        Private Sub UndoRedoMethod(ByVal parameter As Object)
            Dim propName As String = CType(parameter, Object())(0)
            Dim value As Object = CType(parameter, Object())(1)
            Dim prop = TypeDescriptor.GetProperties(Me)(propName)
            changingfromundoredo = True
            prop.SetValue(Me, value)
            changingfromundoredo = False
        End Sub

    End Class
End Namespace
